<template>
    <div>
        <!-- 搜索区 -->
        <el-form :inline="true" size="small" ref="form" label-width="80px" >
            <el-form-item label="物品类型">
                <el-select v-model="params.type" placeholder="请选择" clearable>
                    <el-option v-for="item in goods" :key="item.id" :label="item.name" :value="item.name"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="关键字">
                <el-input v-model="params.name" placeholder="请输入关键字" clearable></el-input>
            </el-form-item>
        </el-form>

        <!-- 表格 -->
        <el-table :data="inventory.list" border style="width: 100%" size="small">
            <el-table-column fixed type="index" label="序号" width="80"></el-table-column>
            <el-table-column fixed prop="asset.name" label="物品名称" width="150"></el-table-column>
            <el-table-column prop="asset.code" label="物品编码" width="120"></el-table-column>
            <el-table-column prop="asset.brand" label="品牌" width="100"></el-table-column>
            <el-table-column prop="asset.specification" label="规格型号" width="120"></el-table-column>
            <el-table-column prop="asset.type" label="类型" width="120"></el-table-column>
            <el-table-column prop="asset.price" label="单价" width="80"></el-table-column>
            <el-table-column prop="asset.num" label="数量" width="80"></el-table-column>
            <el-table-column prop="date" label="介绍" width="300"></el-table-column>
            <el-table-column label="操作" align="center" width="120" fixed="right">
                <template slot-scope="scope">
                    <el-link @click="receiveHandler(scope.row)" :underline="false">领用</el-link>
                    <el-link class="borrow" @click="borrowHandler(scope.row.id)" :underline="false">借用</el-link>
                    <el-link @click="purchaseHandler(scope.row.id)" :underline="false">申购</el-link>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-pagination
            layout="total, prev, pager, next"
            @current-change="pageHandler"
            :page-size="inventory.pageSize"
            :current-page="inventory.page"
            :total="inventory.total">
        </el-pagination>

        <!-- 物品领用 -->
        <el-dialog title="物品领用" :visible.sync="dialogVisible" width="60%">
            {{form}}
            <el-form ref="form" :model="form" label-width="100px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="申请人">
                            {{user.realname}}
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="申请部门" v-if='rows.department'>
                            {{rows.department.name}}
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="申请日期">
                            {{rows.applyforTime | dateFormat}}
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="物品名称" v-if="rows.asset">
                            {{rows.asset.name}}
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-form-item label="申请数量">
                        <el-input v-model="form.num"></el-input>
                    </el-form-item>
                </el-row>
                <el-row>
                    <el-form-item label="申请是由">
                        <el-input type="textarea" v-model="form.reason"></el-input>
                    </el-form-item>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveStorageHandler">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 物品借用 -->
        <el-dialog title="物品借用" :visible.sync="borrowDialogVisible" width="60%">
            {{form}}
            <el-form ref="form" :model="form" label-width="100px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="申请人">
                            {{user.realname}}
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="申请部门" v-if="rows.department">
                            {{rows.department.name}}
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="申请日期">
                            {{rows.applyforTime | dateFormat}}
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="物品名称" v-if="rows.asset">
                            {{rows.asset.name}}
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-form-item label="预计归还日期">
                        <el-date-picker v-model="form.estimatedReturnTime" type="date" placeholder="选择日期" />
                    </el-form-item>
                </el-row>
                <el-row>
                    <el-form-item label="申请数量">
                        <el-input v-model="form.num"></el-input>
                    </el-form-item>
                </el-row>
                <el-row>
                    <el-form-item label="申请是由">
                        <el-input type="textarea" v-model="form.reason"></el-input>
                    </el-form-item>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="borrowDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveStorageHandler">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 物品申购 -->
        <el-dialog title="物品申购" :visible.sync="purchaseDialogVisible" width="60%">
            {{form}}
            <el-form ref="form" :model="form" label-width="100px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="申请人">
                            {{user.realname}}
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="申请部门" v-if="rows.department">
                            {{rows.department.name}}
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="申请日期">
                            {{rows.applyforTime | dateFormat}}
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="物品名称" v-if="rows.asset">
                            {{rows.asset.name}}
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-form-item label="预计使用日期">
                        <el-date-picker v-model="form.estimatedUseTime" type="date" placeholder="选择日期" />
                    </el-form-item>
                </el-row>
                <el-row>
                    <el-form-item label="申请数量">
                        <el-input v-model="form.num"></el-input>
                    </el-form-item>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="预计单价">
                            <el-input v-model="form.estimatedPrice"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="预计总价">
                            <el-input v-model="form.estimatedTotal"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-form-item label="申请是由">
                        <el-input type="textarea" v-model="form.reason"></el-input>
                    </el-form-item>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="purchaseDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveStorageHandler">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 详情 -->
        <el-dialog title="物品详情" :visible.sync="detailsDialogVisible" width="50%">
            <el-form ref="form" :model="detailsForm" label-width="80px">
                <el-form-item label="物品编号">
                    <div>{{goods_row.code}}</div>
                </el-form-item>
                <el-form-item label="物品名称">
                    <div>{{goods_row.name}}</div>
                </el-form-item>
                <el-form-item label="品牌">
                    <div>{{goods_row.brand}}</div>
                </el-form-item>
                <el-form-item label="规格参数">
                    <div>{{goods_row.specification}}</div>
                </el-form-item>
                <el-form-item label="类型">
                    <div>{{goods_row.type}}</div>
                </el-form-item>
                <el-form-item label="单价">
                    <div>{{goods_row.price}}</div>
                </el-form-item>
                <el-form-item label="数量">
                    <div>{{goods_row.num}}</div>
                </el-form-item>
                <div class="details_img">
                    <img :src="goods_row.picture" alt="">
                </div>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="detailsDialogVisible = false">关闭</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import request from '@/utils/request'
import qs from 'qs'
import { mapActions ,mapState} from 'vuex'
export default {
    data() {
        return {
            fileList:[],
            inventory:[],
            params: {
                page: 1,
                pageSize: 5
            },
            goods: [],
            dialogVisible: false,
            borrowDialogVisible: false,
            purchaseDialogVisible: false,
            form: {},
            userInfo:'',
            detailsForm: {},
            detailsDialogVisible: false,
            goods_row: '',
            batchDialogVisible: false,
            batchForm: {},
            rows: []
        }
    },
    mounted() {
        
    },
    created() {
        
        // 分页查询物品信息
        this.queryAsset(this.params)

        // 查询所有物品类型
        this.findAllGoods()

        // 获取经办人信息
        this.getInfo()
    },
    computed: {
        ...mapState('user',['user'])
    },

    watch: {
        params: {
            handler: function() {
                this.queryAsset(this.params)
            },
            deep: true
        }
    },

    methods: {
        ...mapActions('user',['getInfo']),
        // 借用
        borrowHandler() {
            this.borrowDialogVisible = true
        },
        // 申购
        purchaseHandler() {
            this.purchaseDialogVisible = true
        },
        // 领用
        receiveHandler(row) {
            this.dialogVisible = true;
            this.rows = row
        },
        // 保存
        saveStorageHandler() {
            this.form.departmentId = this.rows.department.id;
            this.form.applyforTime = this.rows.applyforTime;
            this.form.assetId = this.rows.asset.id;
            this.form.userId = this.user.id;
            console.log(this.form)
            request.post("/applyfor/saveOrUpdate",qs.stringify(this.form)).then(res => {
                console.log(res)
                this.$message({
                    message: res.message,
                    type: 'success'
                })
                this.dialogVisible = false;
                this.borrowDialogVisible = false;
                this.purchaseDialogVisible = false;
                this.queryAsset(this.params)
            })
        },

        // 查询所有物品类型
        findAllGoods() {
            request.get("/assetType/findAll").then(res => {
                this.goods = res.data
            })
        },
        // 分页查询
        queryAsset() {
            request.post("/applyfor/pageQuery",qs.stringify(this.params)).then(res => {
                this.inventory = res.data
            })
        },

        // 分页
        pageHandler(page) {
            this.params.page = page
        }
    }
}
</script>

<style scoped>
    .borrow {
        margin: 0 .5rem;
    }
    .el-row {
        margin-bottom: 1rem;
    }
    .warn {
        color: #ff0000;
        font-size: 12px;
    }
    .download {
        color: #51c1d9;
        margin: 1rem 0 1rem 1rem;
    }
    .download:hover {
        color: #85d3e4;
    }
    .el-table {
        margin-top: 1rem;
    }
    .el-form-item >>> .el-form-item__label {
        height: 30px;
    }
    .el-form-item__content {
        height: 30px;
    }
    .details_img {
        /* float:right; */
        border: 1px solid #ccc;
        width: 120px;
        height: 120px;
        position: absolute;
        top: 6rem;
        right: 4rem;
        border-radius: 5px;
        padding: 5px;
    }
    .details_img img {
        width: 110px;
        height: 110px;
    }
    .el-dialog__body {
        position: relative;
    }
    .el-form-item {
        margin-bottom: 0;
        height: 30px;
    }
    .el-icon-edit {
        margin: 0 .5rem;
    }
    .el-dialog__wrapper >>> .el-dialog__header{
        background-color: #55c1d9;
    }
    .el-dialog__wrapper >>> .el-dialog__header span,.el-dialog__wrapper >>> .el-icon-close{
        color: #fff;
    }
</style>